import json
import time
import configparser
import os

hour = 1 * 60 * 60
day = hour * 24
week = day * 7
month = week * 4
year = month * 12


def get_now_time():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())


def get_config(param, path='./config/config.ini'):
    conf = configparser.ConfigParser()
    if not os.path.exists(path):
        path = '../config/config.ini'
    conf.read(path)
    try:
        _res = conf.get("config", param)
        return _res
    except configparser.NoOptionError:
        return None


def to_abspath(path):
    return os.path.abspath(path)


def get_parent_path(parent="."):
    return os.path.abspath(os.path.join(os.path.dirname(__file__), parent))


def get_sqlite_path():
    if not os.path.exists(to_abspath(os.path.join("./", get_config("sqlite_path")))):
        return to_abspath(os.path.join("../", get_config("sqlite_path")))
    return to_abspath(os.path.join("./", get_config("sqlite_path")))


def json_to_dict(json_data):
    return json.loads(json_data)


def get_dict_code(dict_data):
    return dict_data["code"]


def get_json_code(json_data):
    return get_dict_code(json_to_dict(json_data))


def is_valid_date(date):
    if date is None:
        return True
    try:
        if ":" in date:
            time.strptime(date, "%Y-%m-%d %H:%M:%S")
        else:
            time.strptime(date, "%Y-%m-%d")
        return True
    except ValueError:
        return False


def str_to_time(str_time, error_text):
    """
    :param str_time: 1 day ago, 2 weeks ago,[hour, hours, day, days, week, weeks, month, months, year, years]
    :param error_text: prompt message of error
    :return: "%Y-%m-%d %H:%M:%S" or False or None
    """
    if str_time is None:
        return None
    if len(str_time.split(" ")) == 3 and str_time.split(" ")[2] == "ago" and str_time.split(" ")[0].isdigit():
        second = 0
        multiple = int(str_time.split(" ")[0])
        str_time_text = str_time.split(" ")[1]
        second = str_to_second(multiple, str_time_text)
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() - second))
    print(error_text)
    print("Error: xx", str_time.split(" ")[1], str_time.split(" ")[2], "only be a positive integer")
    return False


def str_to_second(multiple, str_time):
    _dict = {
        "hour": hour,
        "day": day,
        "week": week,
        "month": month,
        "year": year,
        "hours": hour * multiple,
        "days": day * multiple,
        "weeks": week * multiple,
        "months": month * multiple,
        "years": year * multiple
    }
    if _dict.get(str_time, None):
        second = _dict[str_time]
        return second
    return 0


def get_timestamp_from_format_time(format_time):
    struct_time = time.strptime(str(format_time), '%Y-%m-%d %H:%M:%S')
    return time.mktime(struct_time)


def get_format_time_to_now_second(format_time):
    _second = time.time() - get_timestamp_from_format_time(format_time)
    return _second

